package com.yzw.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.common.domain.EduAclPermission;
import com.yzw.common.domain.EduAclRolePermission;
import com.yzw.common.mapper.EduAclPermissionMapper;
import com.yzw.common.mapper.EduAclRolePermissionMapper;
import com.yzw.common.service.EduAclPermissionService;
import com.yzw.common.service.EduAclRolePermissionService;
import com.yzw.common.vo.MenuVo;
import com.yzw.common.vo.MetaVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class EduAclPermissionServiceImpl extends ServiceImpl<EduAclPermissionMapper, EduAclPermission>
        implements EduAclPermissionService {
    @Resource
    EduAclPermissionMapper eduAclPermissionMapper;
    @Resource
    EduAclRolePermissionService eduAclRolePermissionService;

    @Override
    public List<EduAclPermission> getAll() {
        return eduAclPermissionMapper.getAll();
    }

    @Override
    public List<MenuVo> getById(String id) {
        List<EduAclPermission> eduAclPermissions = eduAclPermissionMapper.getById(id);
        List<MenuVo> menuVos = new ArrayList<>();
        for (EduAclPermission permission : eduAclPermissions){
            MenuVo menuVo = new MenuVo();
            menuVo.setId(permission.getId());
            menuVo.setPid(permission.getPid());
            menuVo.setPath(permission.getPath());
            menuVo.setComponent(permission.getComponent());
//            menuVo.setRedirect(permission.getPermissionValue());
            menuVo.setMeta(new MetaVo());
            menuVo.getMeta().setTitle(permission.getName());
            menuVo.getMeta().setIcon(permission.getIcon());
            menuVos.add(menuVo);
        }
        List<MenuVo> resultMenuVo = new ArrayList<>();
        for (MenuVo menuVo : menuVos) {
            menuVo.setChildren(new ArrayList<>());
            if (menuVo.getPid().equals("1")){
                resultMenuVo.add(selChildren(menuVo,menuVos));
            }
        }
        return resultMenuVo;
    }

    private MenuVo selChildren(MenuVo menuVo, List<MenuVo> menuVos) {
        for (MenuVo vo : menuVos) {
            if (menuVo.getId().equals(vo.getPid())){
                menuVo.getChildren().add(selChildren(vo,menuVos));
            }
        }
        return menuVo;
    }


    @Override
    public List<EduAclPermission> selAllPermission() {
        QueryWrapper<EduAclPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<EduAclPermission> eduAclPermissions = baseMapper.selectList(queryWrapper);
        List<EduAclPermission> eduAclPermissionList = selPermission(eduAclPermissions);
        return eduAclPermissionList;
    }

    @Override
    public void removePermission(String id) {
        // 用于存储该父级的所有子级
        List<String> itList = new ArrayList<>();
        this.selectChildrenById(id, itList);
        itList.add(id);
        baseMapper.deleteBatchIds(itList);
    }

    @Override
    public void rolePermission(String roleId, String[] permissionIds) {
        List<EduAclRolePermission> rolePermissions = new ArrayList<>();
        for (String permissionId : permissionIds) {
            EduAclRolePermission eduAclRolePermission = new EduAclRolePermission();
            eduAclRolePermission.setRoleId(roleId);
            eduAclRolePermission.setPermissionId(permissionId);
            rolePermissions.add(eduAclRolePermission);
        }
        eduAclRolePermissionService.saveBatch(rolePermissions);
    }

    private void selectChildrenById(String id, List<String> itList) {
        QueryWrapper<EduAclPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        queryWrapper.select("id");
        List<EduAclPermission> eduAclPermissions = baseMapper.selectList(queryWrapper);
        eduAclPermissions.stream().forEach(item -> {
            itList.add(item.getId());
            this.selectChildrenById(item.getId(),itList);
        });
    }

    public static List<EduAclPermission> selPermission(List<EduAclPermission> eduAclPermissions) {
        List<EduAclPermission> permissionsNode = new ArrayList<>();
        for (EduAclPermission permissionNode : eduAclPermissions) {
            if ("0".equals(permissionNode.getPid())) {
                permissionNode.setLevel(1);
                permissionsNode.add(selectChildren(permissionNode, eduAclPermissions));
            }
        }
        return permissionsNode;
    }

    public static EduAclPermission selectChildren(EduAclPermission permissionNode,
                                                  List<EduAclPermission> eduAclPermissions) {
        // 初始化
        permissionNode.setChildren(new ArrayList<EduAclPermission>());
        for (EduAclPermission it : eduAclPermissions) {
            if (permissionNode.getId().equals(it.getPid())) {
                int level = permissionNode.getLevel() + 1;
                it.setLevel(level);
                if (permissionNode.getChildren() == null) {
                    permissionNode.setChildren(new ArrayList<EduAclPermission>());
                }
                permissionNode.getChildren().add(selectChildren(it, eduAclPermissions));
            }
        }
        return permissionNode;
    }


}
